package com.team.game.Game.Towers;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;

import com.team.game.framework.Abstract.Tower;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.scale;

public class TowerMelee extends Tower {

    TowerMelee (RectF pos, Game game) {
        super (pos, game);

        recharge = 0.5f; //секунды
        kick = 20;
        radius = (int) (300 * scale);
        radius1 = 0;
        speed = 55 * scale;

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/boom.png");
            boom = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/Towers/test_tower_1.png");
            tower = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/test_attack.png");
            attack = BitmapFactory.decodeStream (inputStream);

        } catch (IOException e) {
            e.printStackTrace ();
        }

    }


    @Override
    public void search (Canvas canvas) {

    }

    @Override
    public void detected (Canvas canvas) {

        canvas.drawBitmap (attack, matrix, null);

    }

    @Override
    public void boom (Canvas canvas) {
        canvas.drawBitmap (boom, null, rectBoom, null);
    }

    @Override
    public void drawTower (Canvas canvas) {
        if (boolMagAnimation) //активується коли башня знаходить моба
        {
            if (time > 0.1) {
                time = 0;
                if (magAnimation == 3) {
                    boolMagAnimation = false;
                    magAnimation = 0;
                    TowerSrc.left = 0;
                    TowerSrc.right = 48;
                } else {
                    magAnimation++;// робимо анімацію моба
                    TowerSrc.left += 48;
                    TowerSrc.right += 48;
                }
            } else
                time += SurfaceThread.deltaTime;

        }
        canvas.drawBitmap (tower, TowerSrc, position, null);
    }

    @Override
    public void towerRestart() {
        search = true;
        setAttack();

    }

    @Override
    public void dispose() {

    }

    @Override
    public boolean rectCompare(RectF rectF) {
        float x2, y2, x1, y1, x3, y3;
        double distance;
        x1 = rectAttack.centerX ();
        y1 = rectAttack.centerY ();
        x2 = rectF.centerX ();
        y2 = rectF.centerY ();

        float difX = x2 - x1;
        float difY = y2 - y1;

        distance = Math.sqrt (Math.pow ((difX), 2.0) + Math.pow (difY, 2.0));
        float scale;
        if ((scale = (float) distance / speed * (float) (187.5 * SurfaceThread.deltaTime)) > 1) {
//             a=  (float)Math.sin((Math.sqrt (Math.pow (difY, 2.0)))/distance);
//             a = (float)Math.toDegrees(Math.asin(a));

            a = (float) ((Math.asin(difX / distance)) / (Math.PI)) * 180;

            x3 = ((difX) / scale);
            y3 = ((difY) / scale); //разберись с нужными/ненужными ректами/переменными

            rectAttack.right += x3;
            rectAttack.left += x3;
            rectAttack.bottom += y3;
            rectAttack.top += y3;

            //если делать только одно преобразование - нужно использовать сет
            //если их несколько - пре и пост
            //на форумах пишут что лучше пост
            matrix.reset();
            matrix.postScale(Scale_X_Y.Scale_X, Scale_X_Y.Scale_Y);
            matrix.postRotate(a, attack.getWidth() / 2, attack.getHeight() / 2);
            matrix.postTranslate(rectAttack.left, rectAttack.top);
            // Attack_Counter++;

            //if (Attack_Counter > 5)
            // Attack_Counter = 0;
            //Log.d (TAG, "x: " + String.valueOf (x3) + " y:" + String.valueOf (y3));
            return false;
        } else {

            //TODO Переделать размеры взрыва
            rectBoom.set(rectF);

            setAttack();
            //только одно пробразование - по этому сет
            //matrix.setTranslate (rectAttack.centerX (), rectAttack.centerY ());

            detected = true;
            return true;
        }
    }

    int i = 0;

    @Override
    public void boom () {
        i++;
        if (i >= 4) {
            time_recharge = System.nanoTime() - time_recharge;
            time_recharge = 0;
            search = true;
            detected = false;
            i = 0;
        }
    }


}